﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace T10_TaskWaitAndContinue
{
    class Program
    {
        static void Main(string[] args)
        {
            //Test1();
            //Test2();

            Test3();
            Console.ReadLine();
        }

        static void Test1()
        {
            Thread thread1 = new Thread(() =>
            {
                Thread.Sleep(2000);
                Console.WriteLine("子线程1\r\n");
            });
            Thread thread2 = new Thread(() =>
            {
                Thread.Sleep(2000);
                Console.WriteLine("子线程2\r\n");
            });

            thread1.Start();
            thread2.Start();

            thread1.Join();
            thread2.Join();

            Console.WriteLine("这个是主线程....");
        }

        static void Test2()
        {
            Task task1 = new Task(new Action(() =>
            {
                Thread.Sleep(2000);
                Console.WriteLine($"Task1子线程Id={Thread.CurrentThread.ManagedThreadId} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");
            }));
            task1.Start();

            Task task2 = new Task(new Action(() =>
            {
                Thread.Sleep(1000);
                Console.WriteLine($"Task2子线程Id={Thread.CurrentThread.ManagedThreadId} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");
            }));
            task2.Start();

            //第1种方式，依次等待
            //task1.Wait();
            //task2.Wait();

            //第2种方式等待所有任务完成
            //Task.WaitAll(task2, task1);

            Task.WaitAny(task1, task2);

            Console.WriteLine($"主线程开始运行! Time={DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");
        }


        static void Test3()
        {
            Task task1 = new Task(() => 
            {
                Thread.Sleep(2000);
                Console.WriteLine($"Task1子线程Id={Thread.CurrentThread.ManagedThreadId} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");
            });
            task1.Start();
            Task task2 = new Task(() => 
            {
                Thread.Sleep(1000);
                Console.WriteLine($"Task2子线程Id={Thread.CurrentThread.ManagedThreadId} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");
            });
            task2.Start();

            //第三个子线程，希望在前面两个子线程“都执行完毕”后再去执行
            //Task.WhenAll(task1, task2).ContinueWith((task) => {
            //    //编写业务逻辑代码

            //    Console.WriteLine($"Task3子线程Id={Thread.CurrentThread.ManagedThreadId} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");
            //});

            Task.WhenAny(task1, task2).ContinueWith((task)=>
            {
                Console.WriteLine($"Task3子线程Id={Thread.CurrentThread.ManagedThreadId} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");
            });


            Console.WriteLine($"主线程开始运行! Time={DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");
        }
    }
}
